package org.sky.security.support;


import org.apache.logging.log4j.Logger;
import org.sky.sys.entity.SysUser;
import org.sky.sys.service.SysUserService;
import org.sky.util.LogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;


/**
 * @author weifx
 * @Title: UserDetailService
 * @ProjectName springboot-platform
 * @Description: TODO{自定义用户服务}
 * @date 2019/2/28 11:29
 * @Version 1.0
 */
@Component
public class CustomUserDetailsService implements UserDetailsService {
    Logger log = LogUtils.getLogger(CustomUserDetailsService.class);
    @Autowired
    private SysUserService sysUserService;
    @Override
    public UserDetails loadUserByUsername(String userCode) throws UsernameNotFoundException {
        //SysUser对应数据库中的用户表，是最终存储用户和密码的表，可自定义
        //本例使用SysUser中的name作为用户名:
        SysUser user = sysUserService.getSysUserByCode(userCode);
        if (user == null) {
            log.info("用户{}登录失败",userCode);
            throw new UsernameNotFoundException("UserCode " + userCode + " not found");
        }
        //SecurityUser实现UserDetails
        SecurityUser seu = new SecurityUser(user);
        return  seu;
    }

}
